#include "qstylesheeteditor.h"
#include "ui_qstylesheeteditor.h"

#include "qstylesheetwidgetfactory.h"
#include "qabstractstylesheetwidget.h"
#include "qstylesheetlist.h"

#include "../minisplitter.h"

#include "../../sharedLib/qabstractproperty.h"
#include "../../sharedLib/qwidgetfactory.h"
#include "../../sharedLib/qabstracthost.h"
#include "../../sharedLib/stylesheet/stylesheetstruct.h"
#include "../../sharedLib/qcommonstruct.h"
#include "../../sharedLib/qpagewidget.h"

#include <QVBoxLayout>
#include <QStringList>
#include <QVariant>

using namespace CORELIB;

QStylesheetEditor::QStylesheetEditor(SHAREDLIB::QAbstractProperty *property, QWidget *parent):
    QDialog(parent),
    ui(new Ui::QStylesheetEditor),
    m_property(property),
    m_stackedWidget(new QStackedWidget(this)),
    m_stylesheetList(new QStylesheetList(this))
{
    ui->setupUi(this);
    m_stackedWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    ui->verticalLayout->insertWidget(0,m_stackedWidget);
    connect(m_stylesheetList,SIGNAL(selectChanged(int)),this,SLOT(showSelect(int)));

    QWidget* wid=new QWidget;
    wid->setLayout(ui->verticalLayout);

    MiniSplitter *s=new MiniSplitter(Qt::Horizontal);
    s->addWidget(m_stylesheetList);
    s->addWidget(wid);

    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);
    v->addWidget(s);
    this->setLayout(v);

    m_stackedWidget->setCurrentIndex(0);

    QObject* obj=m_property->object();

    if(property->object()->inherits("SHAREDLIB::QPageWidget"))
    {
        m_widget=new SHAREDLIB::QPageWidget(property->object()->property("type").toString());
    }
    else
    {
        m_widget=SHAREDLIB::QWidgetFactory::createWidget(obj->property("type").toString()
                                       ,obj->property("plugin").toString());
    }

    SHAREDLIB::QAbstractHost *host=(SHAREDLIB::QAbstractHost*)m_widget->property("host").toInt();
    SHAREDLIB::QAbstractHost *temp=(SHAREDLIB::QAbstractHost*)obj->property("host").toInt();
    QList<SHAREDLIB::QAbstractProperty*>  l=temp->propertys();
    foreach(SHAREDLIB::QAbstractProperty* p,l)
    {
        host->setPropertyValue(p->attribute("name").toString(),p->getValue());
    }
    m_widget->setProperty("no-ManhattanStyle",true);
    host->makeStylesheet();
    QSize sz=m_widget->size();
    sz=sz.boundedTo(ui->frame->size());
    m_widget->setFixedSize(sz);
    m_widget->setParent(ui->frame);
    m_widget->move(ui->frame->width()/2-sz.width()/2,ui->frame->height()/2-sz.height()/2);


    StyleItems  items=m_property->attribute("items").value<StyleItems>();

    foreach(tagStyleItem item,items)
    {
        addItem(&item);
    }

    makeStylesheet();
}

QStylesheetEditor::~QStylesheetEditor()
{
    delete ui;
}

void QStylesheetEditor::addItem(tagStyleItem *it)
{
    tagStylesheetItems items=m_property->getValue().value<tagStylesheetItems>();
    QAbstractStylesheetWidget* wid=m_widgets.value(it->m_string);
    if(wid==NULL)
    {
        wid=QStylesheetWidgetFactory::createWidget(m_property->attribute("name").toString());
        if(wid!=NULL)
        {
            wid->setType(m_property->attribute("name").toString());
            m_stackedWidget->addWidget(wid);
            m_stylesheetList->addItem(it->m_text);
            m_widgets.insert(it->m_string,wid);
            foreach(tagStylesheetItem item,items)
            {
                if(item.m_attributes.value("type").toString()==it->m_string)
                {
                    wid->setItem(item);
                    wid->setDefault(item);
                    break;
                }
            }
            connect(wid,SIGNAL(stylesheetChanged()),this,SLOT(makeStylesheet()));
        }
    }
}

void QStylesheetEditor::makeStylesheet()
{
    QMapIterator<QString,QAbstractStylesheetWidget*>  it(m_widgets);

    tagStylesheetItems items;
    while(it.hasNext())
    {
        it.next();
        tagStylesheetItem item;
        item=it.value()->getItem();
        item.m_attributes.insert("type",it.key());
        items.append(item);
    }

    QVariant v;
    v.setValue<tagStylesheetItems>(items);
    SHAREDLIB::QAbstractHost *host=(SHAREDLIB::QAbstractHost*)m_widget->property("host").toInt();
    host->setPropertyValue(m_property->attribute("name").toString(),v);
    host->makeStylesheet();
}

void QStylesheetEditor::showSelect(int index)
{
    m_stackedWidget->setCurrentIndex(index);
}

void CORELIB::QStylesheetEditor::on_okBtn_clicked()
{
    QMapIterator<QString,QAbstractStylesheetWidget*>  it(m_widgets);

    tagStylesheetItems items;

    while(it.hasNext())
    {
        it.next();
        tagStylesheetItem item;
        item=it.value()->getItem();
        item.m_attributes.insert("type",it.key());
        items.append(item);
        it.value()->takeResource();
    }

    QVariant v;
    v.setValue<tagStylesheetItems>(items);
    m_property->setValue(v);
    close();
}

void CORELIB::QStylesheetEditor::on_cancelBtn_clicked()
{
    close();
}

void CORELIB::QStylesheetEditor::on_resetBtn_clicked()
{
    QMapIterator<QString,QAbstractStylesheetWidget*>          it(m_widgets);
    while(it.hasNext())
    {
        it.next();
        it.value()->reset();
    }

    makeStylesheet();
}

void CORELIB::QStylesheetEditor::on_clearBtn_clicked()
{
    QMapIterator<QString,QAbstractStylesheetWidget*>          it(m_widgets);
    while(it.hasNext())
    {
        it.next();
        it.value()->clear();
    }
    makeStylesheet();
}
